package com.mall.ibatis.persistence.sqlmapdao;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.util.List;

import com.mall.ibatis.bean.EFunction;
import com.mall.ibatis.bean.EFunctionHis;
import com.mall.ibatis.bean.Function;
import com.mall.ibatis.bean.FunctionMenu;
import com.mall.ibatis.persistence.iface.FunctionMenuDAO;

import com.ibatis.dao.client.DaoManager;

public class FunctionMenuSqlMapDAO extends BaseSqlMapDao implements FunctionMenuDAO
{
	public FunctionMenuSqlMapDAO(DaoManager daoManager)
	{
		super(daoManager);
	}
	
	@SuppressWarnings("unchecked")
	public List<FunctionMenu> selectAllFunctionMenu() throws Exception
	{
		return (List<FunctionMenu>)queryForList("select_functionmenu_list",null);
	}
	
	public FunctionMenu selectFunctionMenu(FunctionMenu functionMenu)  throws Exception
	{
		return (FunctionMenu)queryForObject("select_functionmenu", functionMenu);
	}
	
	public String insertFunctionMenu(FunctionMenu functionMenu) throws Exception
	{
		return (String)insert("insert_functionmenu",  functionMenu);
	}
	public void updateFunctionMenu(FunctionMenu functionMenu) throws Exception
	{
		update("update_functionmenu",  functionMenu);
	}
	
	public void deleteFunctionMenu(FunctionMenu functionMenu) throws Exception
	{
		update("delete_functionmenu",  functionMenu);
		
	}

	
	public EFunction selectEFunction(EFunction efunction) throws Exception {
		// TODO Auto-generated method stub
		return  (EFunction)queryForObject("select_EFunction",efunction);
	}

	
	public int insertEFunction(EFunction function,String demand,String detial,String test) throws Exception {
		// TODO Auto-generated method stub
		int flag = 1;
		try {
			daoManager.startTransaction();
			insert("insert_EFunction_forUpdate",function);
			function = (EFunction)queryForObject("select_EFunction_forUpdate",function);
//			System.out.println("-------dao---------------"+demand);
			//获取blob对象
			Blob demandDOC1 = (Blob)function.getDemandDOC1();
			//产生输出流
			OutputStream outStream1 = demandDOC1.setBinaryStream(0);
			File demandeFile = new File(demand);
			FileInputStream fin1 = new FileInputStream(demandeFile);
			//将输入流写到输出流
			byte[] b1 = new byte[fin1.available()];
			fin1.read(b1);
			outStream1.write(b1);
		    //关闭输入输出流
			outStream1.flush();
    		fin1.close();
			outStream1.close();
			
			Blob detialDOC1 = (Blob)function.getDetialDOC1();
			OutputStream outStream2 = detialDOC1.setBinaryStream(0);
			File detialFile = new File(detial);
			FileInputStream fin2 = new FileInputStream(detialFile);
			byte[] b2 = new byte[fin2.available()];
			fin2.read(b2);
			outStream2.write(b2);
			outStream2.flush();
    		fin2.close();
			outStream2.close();
			
			Blob testDOC1 = (Blob)function.getTestDOC1();
			OutputStream outStream3 = testDOC1.setBinaryStream(0);
			File testFile = new File(test);  
			FileInputStream fin3 = new FileInputStream(testFile);
			byte[] b3 = new byte[fin3.available()];
			fin3.read(b3);
			outStream3.write(b3);
		    outStream3.flush();
    		fin3.close();
			outStream3.close();
			
			//修改更新
		    function.setDemandDOC1(demandDOC1);
		    function.setDetialDOC1(detialDOC1);
		    function.setTestDOC1(testDOC1);
		    
		    update("update_EFunction",function);

			daoManager.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
			flag = 0;
		}finally{
			daoManager.endTransaction();
		}
		return flag;
	}

	public EFunction selectEFunctionForUpdate(EFunction function)
			throws Exception {
		// TODO Auto-generated method stub
		EFunction ef=new EFunction();
		try {
			daoManager.startTransaction();
			ef = (EFunction)queryForObject("select_EFunction_forUpdate",function);
			daoManager.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			daoManager.endTransaction();
		}
		return ef;
	}

	
	public int updateEFunction(EFunction function,String demand,String detial,String test) throws Exception {
		// TODO Auto-generated method stub
		int sum=0;
		try {
			daoManager.startTransaction();
			//得到原始对象 
		    EFunction	function1 = (EFunction)queryForObject("select_EFunction_forUpdate",function);
		    //创建历史对象
		    EFunctionHis eFunctionHis = new EFunctionHis();
		    eFunctionHis.setFunctionID(function1.getFunctionID());
		    eFunctionHis.setDescribe(function1.getDescribe());
		    eFunctionHis.setCreateDate(function1.getCreateDate());
		    eFunctionHis.setCreater(function1.getCreater());
		    eFunctionHis.setDemandName(function1.getDemandName());
		    eFunctionHis.setDetialName(function1.getDetialName());
		    eFunctionHis.setTestName(function1.getTestName());
		    //将原始对象修改成修改后
		    function1.setFunctionID(function.getFunctionID());
		    function1.setCreater(function.getCreater());
		    function1.setCreateDate(function.getCreateDate());
		    function1.setDescribe(function.getDescribe());
		    if(demand.length()!=0){function1.setDemandName(function.getDemandName());}
		    if(detial.length()!=0){function1.setDetialName(function.getDetialName());}
		    if(test.length()!=0){function1.setTestName(function.getTestName());}
		    
			//获取blob对象
		    if(demand.length()!=0){
		    	Blob demandDOC1 = (Blob)function1.getDemandDOC1();
//		    	System.out.println("--------------"+demandDOC1.length());
				//产生输出流
				OutputStream outStream1 = demandDOC1.setBinaryStream(0);
				File demandeFile = new File(demand);
				FileInputStream fin1 = new FileInputStream(demandeFile);
				//将输入流写到输出流
				byte[] b1 = new byte[fin1.available()];
				fin1.read(b1);
				outStream1.write(b1);
				//修改更新
				function1.setDemandDOC1(demandDOC1);
			    //关闭输入输出流
				outStream1.flush();
	    		fin1.close();
				outStream1.close();
		    }
			
			if(detial.length()!=0){
				Blob detialDOC1 = (Blob)function1.getDetialDOC1();
				OutputStream outStream2 = detialDOC1.setBinaryStream(0);
				File detialFile = new File(detial);
				FileInputStream fin2 = new FileInputStream(detialFile);
				byte[] b2 = new byte[fin2.available()];
				fin2.read(b2);
				outStream2.write(b2);
				function1.setDetialDOC1(detialDOC1);
				outStream2.flush();
	    		fin2.close();
				outStream2.close();
			}
			
			if(test.length()!=0){
				Blob testDOC1 = (Blob)function1.getTestDOC1();
				OutputStream outStream3 = testDOC1.setBinaryStream(0);
				File testFile = new File(test);  
				FileInputStream fin3 = new FileInputStream(testFile);
				byte[] b3 = new byte[fin3.available()];
				fin3.read(b3);
				outStream3.write(b3);
				function1.setTestDOC1(testDOC1);
			    outStream3.flush();
	    		fin3.close();
				outStream3.close();
			}
			
		    int i=(Integer)update("update_EFunction_All",function1);
		    //插入历史记录
		    String m = (String)insert("insert_EFunctionHis",eFunctionHis);
		    int j = 0;
		    if(m!=null){j =1;}
		    sum = i+j;
			daoManager.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			daoManager.endTransaction();
		}
		return sum;
	}


	public EFunction downLoadDOC(EFunction function, String downFile)
			throws Exception {
		// TODO Auto-generated method stub
		EFunction efunction = (EFunction)queryForObject("select_downFile",function);
		return efunction;
	}
	
	public Function selectFunctionName(Function function)
	throws Exception {
		// TODO Auto-generated method stub
		Function function1 = (Function)queryForObject("select_function_name",function);
		return function1;
	}
	
	public int deleteEFunction(EFunction function)
	throws Exception {
		// TODO Auto-generated method stub
		int i=0;
		int j=0;
		int sum = 0;
		try {
			daoManager.startTransaction();
			 j = delete("delete_e_function_his",function);
			 i = delete("delete_EFunction",function);
			daoManager.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			daoManager.endTransaction();
		}
		sum = i+j;
		return sum;
	}
}
